package com.rcw.commonlib;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import androidx.annotation.RequiresApi;
import androidx.gridlayout.widget.GridLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.rcw.filterpopup.R;
import com.rcw.popup.FilterBean;
import com.rcw.popup.LabelGridLayout;
import com.rcw.popup.SingleFilterBean;
import com.rcw.popup.TypeLabelGridLayout;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by ruancw on 2018/5/31.
 * 自定义类似流式布局的筛选popupWindow
 */

public class MulFilterPopup extends PopupWindow{

    private MulFilterPopup(Context context, View view){
        //这里可以修改popupWindow的宽高
        super(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        setContentView(view);
        //设置popupWindow弹出和消失的动画效果
        //setAnimationStyle(R.style.popwin_anim_style);
        //设置有焦点
        setFocusable(true);
        setBackgroundDrawable(new BitmapDrawable());
        //设置点击外部可消失
        setOutsideTouchable(true);
    }

    /**
     * 重写showAsDropDown方法，解决高版本不在控件下方显示的问题
     * @param anchor popupWindow要显示在的控件
     */
    @Override
    public void showAsDropDown(View anchor) {
        if(Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    /**
     * 自定义的内部类，用于设置参数
     *
     */
    public static class Builder {
        private Context context;//上下文对象
        private List<FilterBean> listData;//要显示的数据集合
        private List<SingleFilterBean> singleFilterBeans;//要显示的数据集合
        private LabelGridLayout mGridLayout;//用于显示流式布局
        private TypeLabelGridLayout mTypeGridLayout;//用于显示流式布局
        private LinearLayout llContent;//popupWindow的内容显示
        //背景颜色
        private int colorBg = Color.parseColor("#FFFFFF");
        //popup背景颜色
        private int popupBg = Color.parseColor("#33000000");
        //按钮背景色
        private int btnBg = R.drawable.shape_circle_bg;
        private MulFilterPopup mFlowPopupWindow;
        private List<String> labelLists=new ArrayList<>();
        private int width;

        public Builder(Context context) {
            this.context = context;
        }

        /**
         * 设置数据集合
         *
         */
        public void setValues(List<FilterBean> listData, List<SingleFilterBean> singleFilterBeans, int width) {
            this.listData = listData;
            this.singleFilterBeans = singleFilterBeans;
            this.width = width;
        }

        /**
         * 设置内容区域的背景色
         * @param color 颜色
         */
        public void setBgColor(int color){
            colorBg = context.getResources().getColor(color);
        }

        /**
         * 设置popup的背景色
         * @param color 颜色
         */
        public void setPopupBgColor(int color){
            popupBg = context.getResources().getColor(color);
        }

        /**
         * 设置按钮的背景色
         * @param btnBg 背景色（color）
         */
        public void setBtnBg(int btnBg) {
            this.btnBg = btnBg;
        }


        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        public void build(){
            //初始化popupWindow的布局文件
            initPopup();

        }

        /**
         * 初始化PopupWindow的布局
         */
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        private void initPopup(){
            //初始化popupWindow的布局文件
            View view = LayoutInflater.from(context).inflate(R.layout.filter_popup,null);
            //主要用于设置数据显示区域的背景色
            LinearLayout ll= (LinearLayout) view.findViewById(R.id.ll);
            //RelativeLayout rlBtn= (RelativeLayout) view.findViewById(R.id.rl_btn);
            //流布局数据展示控件
            mGridLayout= (LabelGridLayout) view.findViewById(R.id.gl_label);
            mTypeGridLayout= (TypeLabelGridLayout) view.findViewById(R.id.gl_type_label);
            //确定按钮
            TextView tvConfirm= (TextView) view.findViewById(R.id.tv_confirm);
            tvConfirm.setBackgroundDrawable(context.getResources().getDrawable(btnBg));
            //设置数据展示区域的背景色
            ll.setBackgroundColor(colorBg);

            llContent = new LinearLayout(context);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            view.setLayoutParams(params);
            llContent.addView(view);
            //设置背景色，不设置的话在有些机型会不显示popupWindow
            llContent.setBackgroundColor(popupBg);

            mTypeGridLayout.setColumnCount(3);
            mTypeGridLayout.setLabelBg(com.rcw.commonlib.R.drawable.flow_popup);
            mTypeGridLayout.setMulEnable(true);
            mTypeGridLayout.setTitleColor(context.getResources().getColor(com.rcw.commonlib.R.color.colorFont));
            mTypeGridLayout.setGridData(listData);

            mGridLayout.setColumnCount(3);
            mGridLayout.setLabelBg(com.rcw.commonlib.R.drawable.flow_popup);
            mGridLayout.setMulEnable(true);
            mGridLayout.setGridData(singleFilterBeans);

            //确定按钮的点击事件
            tvConfirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //监听接口的数据回调方法
                    flowPopupMonitor.setFlowPopupResult(labelLists);
                    //隐藏popupWindow
                    hidePopup();
                }
            });
        }

        /**
         * 隐藏popupWindow
         */
        private void hidePopup() {
            if (mFlowPopupWindow != null&&mFlowPopupWindow.isShowing()){
                mFlowPopupWindow.dismiss();
            }
        }

        /**
         * 创建popupWindow
         * @return FlowPopupWindow实例
         */
        public MulFilterPopup createPopup(){
            if (listData == null || listData.size() == 0){
                try {
                    throw new Exception("没有筛选标签");
                } catch (Exception e) {
                    Toast.makeText(context,e.getMessage(),Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
                return null;
            }
            mFlowPopupWindow = new MulFilterPopup(context,llContent);
            return mFlowPopupWindow;
        }

    }

    private static FlowPopupMonitor flowPopupMonitor;

    public interface FlowPopupMonitor{
        void setFlowPopupResult(List<String> filterResult);
    }

    public void setFlowPopupMonitor(FlowPopupMonitor flowPopupMonitor){
        this.flowPopupMonitor=flowPopupMonitor;
    }

}
